<?php
namespace Library;
class toolOrder{
     public function QSorderXD($XDnumber=null){//亲算下单
         if($_SERVER['HTTP_HOST']==cookie('jieguoUrlYM')){
             return 123;
         }

         $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。

         $orderid=cookie('orderid');
         $Zorderid=cookie('orderid');
         cookie('Zorderid',$Zorderid,3600);
         $uid=cookie('uid');
         $price=cookie('price');
         $channel=cookie('channel');

//         $arrdata[0]=S('sm_h5test|'.$Zorderid);
//         if(empty($arrdata[0]['ordernum'])){
//             $arrdata=M()->query("select * from sm_h5test where ordernum='{$Zorderid}'");
//         }

         //容错渠道等信息
//         channelFaultHandle($arrdata[0]);

//         $createtime=$arrdata[0]['createtime'];
//         if($arrdata[0]['status']==1&&$arrdata[0]['paykind']==-1){
//             M()->query("update sm_h5test set status=-1 where ordernum='{$arrdata['ordernum']}'");
//             redirect(U('Index/mycesuan','',false));exit;
//         }


//         if($arrdata[0]['status']<1){
//             redirect(U('Index/mycesuan','',false));exit;
//         }


//         if(cookie('selectOrderJG'.$Zorderid)==''){
//             cookie('selectOrderJG'.$Zorderid,1,3600);
//             M()->query("update sm_h5test set status=1 where ordernum='{$Zorderid}'");
//         }

         //分销接口失败，重新回调
//         if($arrdata[0]['areaes']=='0|fenxiaotonji'){
//             fenxiaotonji($arrdata[0]['channel'],$Zorderid,$price,$createtime,$uid);//分销统计回调
//         }

         //取消亲算订单
         $QSorderNo=cookie('QSorderNo');
         if($QSorderNo==1){
             return 0;
         }

         if(channelxinbang($channel)){//合作新榜判断
             cookie('QSorderNo',1);
         }


         if (strpos($user_agent, 'MicroMessenger') == false) {
             //非微信浏览器访问
         }else{
             //跳转域名授权判断
             $openid=cookie('openid');
             if(empty($openid)){
                 cookie('QSorderNo',1);
                 return 0;
             }
         }

         if (strpos($user_agent, 'MicroMessenger') == false&&empty($XDnumber)) {
             //非微信浏览器访问
             return 0;
         }

         $strData=mb_substr($orderid,0,2);
         $ZstrData=mb_substr($Zorderid,0,2);
         if($ZstrData=="QS"){//多次点击
             $orderid=$ZstrData;
         }else{
             if($strData!="QS"){
                 $orderid='QS'.date('Ymd').mt_rand(10000,99999).date('His');
                 cookie('orderid',$orderid);
             }
         }

         //创建订单
         $hour=cookie('zhour');
         $shop=$orderid;
         $znickname=cookie('znickname');
         $zsex=cookie('zsex');if(empty($zsex)){$zsex=1;}
         $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
         $y=mb_substr(cookie('zymd'),0,4);//年
         $m=mb_substr(cookie('zymd'),5,2); //月
         $d=mb_substr(cookie('zymd'),8,2);//日
         cookie('price',188);//价格
         $typename=cookie('csTypePrice');
         $orderidnum=cookie('orderidnum');
//         if($typename=='158'){//其他价格
//             cookie('price',158);//价格
//         }
         if($orderidnum=='Bzhh'||$orderidnum=='Hyzs'){
             cookie('price','288');
         }
//         $datastr=stristr(cookie('znickname'),'测试');
//         if($datastr){cookie('price',1,604800);}
         $price=cookie('price');


         $strData=mb_substr($shop,0,2);
         S('sm_h5test|',null);
         $zarrdata[0]=S('sm_h5test|'.$Zorderid);
         $arrdata[0]=S('sm_h5test|'.S('date_remark'.$Zorderid));

         if($strData=='QS'&&$ZstrData!="QS"){
             $smH5testinter=array(
                 'ordernum'=>$shop,
                 'price'=>$price,
                 'username'=>$znickname,
                 'typeid'=>26,
                 'sex'=>$zsex,
                 'createtime'=>date('Y-m-d H:i:m'),
                 'status'=>-1,
                 'datetype'=>0,
                 'year'=>$y,
                 'month'=>$m,
                 'day'=>$d,
                 'hour'=>$hour,
                 'date_remark'=>$Zorderid,
                 'appuserid'=>$imei,
                 'ip'=>$_SERVER['REMOTE_ADDR'],
                 'channel'=>$channel,
                 'paykind'=>-1,
                 'auto_send_sms'=>0,
             );
             if(empty($arrdata[0])&&$zarrdata[0]['place']<1){
//                 M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,appuserid,ip,channel,auto_send_sms)values(
//                                              '{$shop}','{$price}','{$znickname}',26,{$zsex},-1,0,{$y},{$m},{$d},{$hour},-1,'{$Zorderid}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}',0)");
                 S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                 S('date_remark'.$Zorderid,$shop,2592000);
                 cacheZhiFuOrderid($shop);//支付页缓存订单操作
             }else if(empty($arrdata[0])&&$zarrdata[0]['place']==1){
                 $smH5testinter['place']=1;
                 S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                 S('date_remark'.$Zorderid,$shop,2592000);
                 cacheZhiFuOrderid($shop);//支付页缓存订单操作
//                 M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,appuserid,ip,channel,place,auto_send_sms)values(
//                                              '{$shop}','{$price}','{$znickname}',26,{$zsex},-1,0,{$y},{$m},{$d},{$hour},-1,'{$Zorderid}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}',1,0)");
             }
         }else{
             //echo '订单错误'.$shop;
         }


         if($arrdata[0]['status']==1){
             return 1;
         }else{//更新数据hu
             if($arrdata[0]['status']==-1&&$strData=='QS'){
                 cacheZhiFuOrderidDelete($arrdata[0]['ordernum']);//缓存订单删除操作
                 $arrdata[0]['ordernum']=$shop;
                 S('sm_h5test|'.$shop,$arrdata[0],2592000);//2592000为30天
                 S('date_remark'.$Zorderid,$shop,2592000);
                 cacheZhiFuOrderid($shop);//支付页缓存订单操作
             }
             if (strpos($user_agent, 'MicroMessenger') == false) {
                 //非微信浏览器访问
             }else{
                 $xml=wxgzhzf('QS','一对一亲算',$orderid);//公众号支付回调参数
                 $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
                 $val = json_decode(json_encode($xmlstring),true);
                 $noncestr=noncestr(15);//随机字符串
                 $timestamp=time();//时间戳


                 $stringA="appId=".C('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
                 $stringSignTemp=$stringA."&key=".C('KEY');
                 $sign=strtoupper(md5($stringSignTemp));

                 $wxzhifuArr=array(
                     'timeStamp'=>$timestamp,
                     'nonceStr'=>$noncestr,
                     'package'=>'prepay_id='.$val['prepay_id'],
                     'signType'=>'MD5',
                     'paySign'=>$sign,
                     'appId'=>C('APPID')
                 );
                 return $wxzhifuArr;
             }
         }

     }

    public function QSdashiXD(){
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。

        $Zorderid=cookie('Zorderid');


        $arrdata[0]=S('sm_h5test|'.$Zorderid);
        if(empty($arrdata[0]['ordernum'])){
            $arrdata=M()->query("select * from sm_h5test where ordernum='{$Zorderid}'");
        }

        //容错渠道等信息
        channelFaultHandle($arrdata[0]);
        $channel=cookie('channel');


        if($arrdata[0]['status']!=1){
            redirect(U('Index/mycesuan','',false));exit;
        }

        $csName=mb_substr($Zorderid,0,2);
        $orderid='QS'.date('Ymd').mt_rand(100,999).date('His');
        cookie('orderid',$orderid);
        //创建订单
        $hour=cookie('zhour');
        $shop=$orderid;
        $znickname=cookie('znickname');
        $zsex=cookie('zsex');if(empty($zsex)){$zsex=1;}
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日

        $price=cookie('price');
        if(empty($price)){cookie('price',138);}


        $strData=mb_substr($shop,0,2);
//         $zarrdata=M()->query("select id,place from sm_h5test where ordernum='{$Zorderid}'");
//         $arrdata=M()->query("select id,status from sm_h5test where date_remark='{$Zorderid}'");
        S('sm_h5test|',null);
        $zarrdata[0]=S('sm_h5test|'.$Zorderid);
        $arrdata[0]=S('sm_h5test|'.S('date_remark'.$Zorderid));

        if($strData=='QS'){
            $smH5testinter=array(
                'ordernum'=>$shop,
                'price'=>$price,
                'username'=>$znickname,
                'typeid'=>26,
                'sex'=>$zsex,
                'createtime'=>date('Y-m-d H:i:m'),
                'status'=>-1,
                'datetype'=>0,
                'year'=>$y,
                'month'=>$m,
                'day'=>$d,
                'hour'=>$hour,
                'date_remark'=>$Zorderid,
                'appuserid'=>$imei,
                'ip'=>$_SERVER['REMOTE_ADDR'],
                'channel'=>$channel,
                'paykind'=>-1,
                'auto_send_sms'=>0,
            );
            if(empty($arrdata[0])&&$zarrdata[0]['place']<1){
//                 M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,appuserid,ip,channel,auto_send_sms)values(
//                                              '{$shop}','{$price}','{$znickname}',26,{$zsex},-1,0,{$y},{$m},{$d},{$hour},-1,'{$Zorderid}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}',0)");
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                S('date_remark'.$Zorderid,$shop,2592000);
                cacheZhiFuOrderid($shop);//支付页缓存订单操作
            }else if(empty($arrdata[0])&&$zarrdata[0]['place']==1){
                $smH5testinter['place']=1;
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                S('date_remark'.$Zorderid,$shop,2592000);
                cacheZhiFuOrderid($shop);//支付页缓存订单操作
//                 M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,date_remark,appuserid,ip,channel,place,auto_send_sms)values(
//                                              '{$shop}','{$price}','{$znickname}',26,{$zsex},-1,0,{$y},{$m},{$d},{$hour},-1,'{$Zorderid}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}',1,0)");
            }
        }else{
            echo '订单错误'.$shop;
        }


        if($arrdata[0]['status']==1){
            return 1;
        }else{//更新数据hu
            if($arrdata[0]['status']==-1&&$strData=='QS'){
                cacheZhiFuOrderidDelete($arrdata[0]['ordernum']);//缓存订单删除操作
                $arrdata[0]['ordernum']=$shop;
                S('sm_h5test|'.$shop,$arrdata[0],2592000);//2592000为30天
                S('date_remark'.$Zorderid,$shop,2592000);
                cacheZhiFuOrderid($shop);//支付页缓存订单操作
                //M()->query("update sm_h5test set ordernum='{$shop}' where id='{$arrdata[0]['id']}'");
            }
        }
    }

}
